<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta charset="UTF-8" />
<title>角色管理</title>

<link rel="stylesheet" type="text/css" href="../Public/jquery-easyui-1.3.4/themes/default/easyui.css">
<link rel="stylesheet" type="text/css" href="../Public/jquery-easyui-1.3.4/themes/icon.css">
<script type="text/javascript" src="../Public/jquery-easyui-1.3.4/jquery.min.js"></script>
<script type="text/javascript" src="../Public/jquery-easyui-1.3.4/jquery.easyui.min.js"></script>
<script type="text/javascript">
$(document).ready(function(){
	$('#mytable').datagrid({
		nowrap: false,
		striped: true,
		url:'<{:U("rolejson")}>',
		loadMsg:'数据正在加载，请稍候...',
		columns:[[
			{field:'table_id',checkbox:true},
			{field:'table_name',title:'角色名称',width:300,align:"left"},
			{field:'table_info',title:'角色描述',width:300,align:"left",formatter:function(value){
				if(value.length > 20){
					value = value.substring(0,20)+"...";
				}
				return value;
			}},
			{field:'table_power',title:'角色权限',width:250,align:"center"},
			{field:'table_adppower',title:'广告位权限',width:250,align:"center"}
		]],
		singleSelect:true,
		fit:true,
		toolbar:[{
			text:'新增角色',
			iconCls:'icon-add',
			handler:addAction
		},'-',{
			text:'编辑角色',
			iconCls:'icon-edit',
			handler:editAction
		},'-',{
			text:'编辑广告位',
			iconCls:'icon-edit',
			handler:editadAction
		},'-',{
			text:'删除角色',
			iconCls:'icon-remove',
			handler:removeAction
		}]
	});
});
function addAction(){
	$("#form_name").val('');
	$("#form_info").val('');
	$('#power').tree({
		checkbox: true,
		url: '<{:U("rolepower")}>'
	});
	$("#formWindow").window('setTitle','新增角色');
	$("#submit").unbind("click");
	$("#submit").bind("click",{action:"<{:U('roleadd')}>"},checkSubmit);
	$("#formWindow").window('open');
	$("#myLayout").layout('resize');
}
function editAction(){
	var selected = $('#mytable').datagrid('getSelected');
	if(!selected){
		$.messager.alert('错误消息','请先选择一个角色再编辑！','error');
	}else{
		$("#form_name").val(selected.table_name);
		$("#form_info").val(selected.table_info);
		$("#formWindow").window('setTitle','编辑角色');
		$("#submit").unbind("click");
		$("#submit").bind("click",{type:"edit",action:'<{:U("roleedit")}>',table_id:selected.table_id},checkSubmit);
		$('#power').tree({
			checkbox: true,
			url: 'index.php?s=/System/rolepower/roleid/'+selected.table_id
		});
		$("#formWindow").window('open');
		$("#myLayout").layout('resize');
	}
}
function editadAction(){
	var selected = $('#mytable').datagrid('getSelected');
	if(!selected){
		$.messager.alert('错误消息','请先选择一个角色再编辑！','error');
	}else{
		$("#form_name").val(selected.table_name);
		$("#form_info").val(selected.table_info);
		$("#formWindow").window('setTitle','编辑角色');
		$("#submit").unbind("click");
		$("#submit").bind("click",{type:"editad",table_id:selected.table_id},checkSubmit);
		$('#power').tree({
			checkbox: true,
			url: 'ajax.php?act=adppower&roleid='+selected.table_id
		});
		$("#formWindow").window('open');
		$("#myLayout").layout('resize');
	}
}
function removeAction(){
	var selected = $('#mytable').datagrid('getSelected');
	if(!selected){
		$.messager.alert('错误消息','请先选择一个角色再删除！','error');
	}else{
		$.messager.confirm('提示消息','确认删除？删除角色同时会删除属于此角色的用户！',function(r){
			if(r){
				$.post('<{:U("roleremove")}>','form_id='+selected.table_id,function(data){
					if(data.status=='false'){
						$.messager.alert('错误消息',data.info,'error');
					}else{
						$.messager.alert('提示消息',data.info,'info',function(){
							$("#mytable").datagrid('reload');
						});
					}
				},"json");
			}
		});
	}
}
function closeForm(){
	$("#formWindow").window('close');
}
function checkSubmit(event){
	if(!$("#myForm").form('validate')){
		$.messager.alert('错误消息','请确保表单信息无误再提交！','error');
	}else{
		var nodes = $('#power').tree('getChecked');
		var s = '';
		for(var i=0; i<nodes.length; i++){
			if (s != '') s += ',';
			s += nodes[i].id;
		}
		closeForm();
		$.post(event.data.action,$('#myForm').serialize()+'&form_id='+event.data.table_id+'&form_power='+s,function(data){
			if(data.status=='false'){
				$.messager.alert('错误消息',data.info,'error');
			}else{
				$.messager.alert('提示消息',data.info,'info',function(){
					$("#mytable").datagrid('reload');
				});
			}
		},"json"); 
	}
}
</script>
</head>
<body class="easyui-layout">
<div region="center" title="系统设置-&gt;角色管理">
<table id="mytable"></table>
</div>
<div id="formWindow" class="easyui-window" title="新增角色" minimizable="false" collapsible="false" maximizable="false" closed="true" modal="true" style="width:500px;height:350px;padding:5px;">
	<div class="easyui-layout" fit="true" id="myLayout">
		<div region="center" border="false">
		<form id="myForm" method="post">
			<table>
				<tr><td>角色名称：</td><td><INPUT class="easyui-validatebox" required="true" missingMessage="角色名称不能为空" TYPE="text" NAME="form_name" id="form_name" style="width:250px"></td></tr>
				<tr><td>描述：</td><td><TEXTAREA class="easyui-validatebox" NAME="form_info" id="form_info" style="width:250px;height:50px;"></TEXTAREA></td></tr>
				<tr><td>权限：</td><td><ul id="power"></ul></td></tr>
			</table>
		</form>
		</div>
		<div region="south" border="false" style="text-align:right;height:30px;line-height:30px;">
			<a class="easyui-linkbutton" icon="icon-ok" href="javascript:void(0)" id="submit">确定</a>
			<a class="easyui-linkbutton" icon="icon-cancel" href="javascript:void(0)" onclick="closeForm()">取消</a>
		</div>
	</div>
</div>
</body>
</html>